package com.xiaoxu.rental.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.rental.entity.AutoInfo;
import com.xiaoxu.rental.service.AutoInfoService;
import com.xiaoxu.rental.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiaoxu
 * @since 2024-07-24
 */
@Api(tags = "汽车信息管理")
@RestController
@RequestMapping("/rental/autoInfo")
@SuppressWarnings("all")
public class AutoInfoController {

    @Value("${auto.info.maintain-mileage}")
    private Integer maintainMileage;
    @Resource
    private AutoInfoService autoInfoService;

    @ApiOperation(value = "分页查询汽车信息")
    @PostMapping("{start}/{size}")
    public Result search(@PathVariable int start, @PathVariable int size,
                         @RequestBody AutoInfo autoInfo){
        Page<AutoInfo> page = new Page<>(start, size);
        return Result.success(autoInfoService.searchByPage(page, autoInfo));
    }

    @ApiOperation(value = "新增汽车信息")
    @PostMapping
    public Result save(@RequestBody AutoInfo autoInfo){
    //设置一下应保次数和实保次数
        int i = autoInfo.getMileage() / maintainMileage;
        autoInfo.setExpectedNum(i);
        autoInfo.setActualNum(i);
        return autoInfoService.save(autoInfo)?
                Result.success():Result.fail();
    }
    @ApiOperation(value = "修改汽车信息")
    @PutMapping
    public Result update(@RequestBody AutoInfo autoInfo){
        int i = autoInfo.getMileage() / maintainMileage;
        autoInfo.setExpectedNum(i);
        autoInfo.setActualNum(i);
        return autoInfoService.updateById(autoInfo)?
                Result.success():Result.fail();
    }
    @ApiOperation(value = "删除汽车信息")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids){
        return Result.success(autoInfoService.delete(ids));
    }


    @PostMapping("exist")
    public Result exist(@RequestBody AutoInfo autoInfo){
        AutoInfo info =
                autoInfoService.selectAutoNum(autoInfo.getAutoNum());
        return ObjectUtil.isNotEmpty(info) ?
                Result.success().setMessage("have"):Result.success().setMessage("none");
    }

    @ApiOperation(value = "查询需要保养的车辆信息")
    @GetMapping("toBeMaintain")
    public Result toBeMaintain(){
        return Result.success(autoInfoService.toBeMaintain());
    }


}
